const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // slide
    value:0,
    maxvalue:'',




    page:1,
    limit:10,

    list:[],
    music:'',
    number:'-1',


    starttime:'00:00',
    endtime:'',
    // 搜索姓名
    name:'',

    // 限制多次点击
    goplay:true,
    musicname:''
  },
// ----
  // 姓名搜索
  name(e){
    let that = this
    that.setData({
      name:e.detail.value,
      page:'1'
    })
    that.musiclist()
  },
  delete(){
    let that = this
    that.setData({
      name:''
    })
    that.musiclist()
  },
// ----
  goplay:function(e){
    let that = this
    if(that.backmusic){
      that.backmusic.pause()
    }
    
    setTimeout(()=>{
      that.setData({
        goplay:true
      })
    },2000)
    if(!that.data.goplay){
      return false;
    }
    that.setData({
      goplay:false
    })
    wx.showLoading({
      title: '跳转中',
    })

    that.setData({
      number:'-1',
    })
    console.log(e);
    let hbdetail = {
      image:e.currentTarget.dataset.image,
      music:e.currentTarget.dataset.music,
      name:e.currentTarget.dataset.name,

    }
    wx.setStorage({
      key: 'tmusic',
      data: hbdetail,
      success: function(res) {
        wx.hideLoading()
        wx.navigateTo({
          url: '../khbypplay/index'
        })
      },
    })
  },


  // 防止冒泡
  mp:function(){
    return false
  },
  // 点击进度条跳动
  sliderchange(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },
  sliderchanging(event){
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },

  // 开始播放
  audioPlay(e){
    console.log(e)
    let that = this

    if(!e.currentTarget.dataset.music){
//    wx.showToast({
//      title:'音频地址不正确',
//      icon:'none',
//      duration:2000
//    })
      return false
    }

    if(that.data.musicname == e.currentTarget.dataset.title){
      that.setData({
        number:e.currentTarget.dataset.index,
      })
      that.backmusic.play()
    }else{
      that.setData({
        music:e.currentTarget.dataset.music,
        number:e.currentTarget.dataset.index,
        musicname:e.currentTarget.dataset.title
      })
      that.music()
    }
    that.backmusic.onTimeUpdate(()=>{
      //console.log(that.backmusic.duration)  //总时长
      //console.log(that.backmusic.currentTime)//进行
        // 计算时长
        let sumtime = that.backmusic.duration;
        let newtime = that.backmusic.currentTime
        let width = newtime / sumtime * 100 + '%'
        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);
        if (sumtimem < 10) {
          sumtimem = "0" + sumtimem
        }
        if (sumtimes < 10) {
          sumtimes = "0" + sumtimes
        }
        let newtimem = parseInt(newtime / 60);
        let newtimes = parseInt(newtime % 60);


        if (newtimem < 10) {
          newtimem = "0" + newtimem
        }
        if (newtimes < 10) {
          newtimes = "0" + newtimes
        }

        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)

      that.setData({
        value:that.backmusic.currentTime,
        maxvalue:that.backmusic.duration,
        starttime:start,
        endtime:end
      })
    })

    // 循环播放
    that.backmusic.onEnded(function(){
      that.music()
    })

  },
  // 停止播放
  audioPause(){
    let that = this
    that.setData({
      number:'-1',
    })
    that.backmusic.pause()
  },



  // 创建背景音乐
  music(){
    let that = this
    that.backmusic = wx.getBackgroundAudioManager()
    that.backmusic.src =  that.data.music
    that.backmusic.title= that.data.musicname
  },


  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.musiclist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let page = that.data.page
    page++
    that.setData({
      page:page
    })
    that.musiclist();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


 
    
      // 获取列表
      musiclist: function () {
        wx.showLoading({
            title: '加载中'
        })
        var that = this;
        wx.getStorage({
            key: 'yzuser',
            success: function (res) {
                console.log(res);
                var setdata = {
                    token: res.data.token,
                    page: that.data.page,
                    limit: that.data.limit,
                    name:that.data.name,
                }
                console.log(setdata);
                wx.request({
                    url: app.api + app.yzgoods,
                    method: 'POST',
                    data: setdata,
                    success(e) {
                        wx.hideLoading();
                        console.log(e);
                        if (e.data.msg == 0) {
                            if (that.data.page == 1) {
                                that.setData({
                                    list: e.data.data.list,
                                })
                                console.log("aaa")
                                wx.hideLoading();
                                // 空的
                                if (!e.data.data.list.length) {
                                    wx.showToast({
                                        title: '暂无数据',
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }

                            } else if (e.data.data.list.length) {
                                let list = that.data.list
                                list = list.concat(e.data.data.list)
                                that.setData({
                                    list: list
                                })
                                wx.hideLoading()
                                console.log(list)
                            } else {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '没有更多了',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }


                        } else if (e.data.msg == -1) {
                            wx.redirectTo({
                                url: '../yzlogin/login'
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
            },
        })
    },
})